import os
import requests
from app import create_app
from app.models.models import db, Team
from urllib.parse import unquote

app = create_app()

def download_icon(url, team_name):
    try:
        # 创建icons目录（如果不存在）
        if not os.path.exists('app/static/team_icons'):
            os.makedirs('app/static/team_icons')
            
        # 从URL中提取文件名
        filename = f"{team_name.lower().replace(' ', '_')}.png"
        filepath = os.path.join('app/static/team_icons', filename)
        
        # 下载图片
        response = requests.get(url)
        if response.status_code == 200:
            with open(filepath, 'wb') as f:
                f.write(response.content)
            return filename
        else:
            print(f"下载失败: {team_name} - 状态码: {response.status_code}")
            return None
    except Exception as e:
        print(f"下载出错: {team_name} - {str(e)}")
        return None

def update_team_icons():
    with app.app_context():
        # 战队数据
        teams_data = [
            {"name": "T1", "league": "LCK", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1726801573959_539px-T1_2019_full_allmode.png"},
            {"name": "Gen.G", "league": "LCK", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1655210113163_GenG_logo_200407-05.png"},
            {"name": "Hanwha Life Esports", "league": "LCK", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1631819564399_hle-2021-worlds.png"},
            {"name": "DN Freecs", "league": "LCK", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1735904500256_DN_Freecslogo_profile.webp"},
            {"name": "KT Rolster", "league": "LCK", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2Fkt_darkbackground.png"},
            {"name": "Nongshim RedForce", "league": "LCK", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2FNSFullonDark.png"},
            {"name": "Dplus KIA", "league": "LCK", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1673260049703_DPlusKIALOGO11.png"},
            {"name": "BNK FEARX", "league": "LCK", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1734691810721_BFXfullcolorfordarkbg.png"},
            {"name": "DRX", "league": "LCK", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1672910733664_01.Basic_W.png"},
            {"name": "OKSavingsBank BRION", "league": "LCK", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1716454325887_Nowyprojekt.png"},
            {"name": "Bilibili Gaming", "league": "LPL", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1682322954525_Bilibili_Gaming_logo_20211.png"},
            {"name": "JD Gaming", "league": "LPL", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1627457924722_29.png"},
            {"name": "Top Esports", "league": "LPL", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1592592064571_TopEsportsTES-01-FullonDark.png"},
            {"name": "Weibo Gaming", "league": "LPL", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1641202879910_3.png"},
            {"name": "EDward Gaming", "league": "LPL", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1631819297476_edg-2021-worlds.png"},
            {"name": "Anyone's Legend", "league": "LPL", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1641199582689_.png"},
            {"name": "LNG Esports", "league": "LPL", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1717487697003_LNGlogo.png"},
            {"name": "FunPlus Phoenix", "league": "LPL", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1627457887494_7_720.png"},
            {"name": "Team WE", "league": "LPL", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1634763008788_220px-Team_WE_logo.png"},
            {"name": "Ultra Prime", "league": "LPL", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2Fultraprime.png"},
            {"name": "ThunderTalk Gaming", "league": "LPL", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2FTT-FullonDark.png"},
            {"name": "Royal Never Give Up", "league": "LPL", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1631819360134_rng-2021-worlds.png"},
            {"name": "Oh My God", "league": "LPL", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1686821355861_OMG_2023_logo-01.png"},
            {"name": "Ninjas in Pyjamas", "league": "LPL", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1673425724696_NIP-Symbol-RGB-NeonYellow1.png"},
            {"name": "LGD Gaming", "league": "LPL", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2FLGD-FullonDark-1.png"},
            {"name": "Invictus Gaming", "league": "LPL", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1634762917340_300px-Invictus_Gaming_logo.png"},
            {"name": "G2 Esports", "league": "LEC", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2FG2-FullonDark.png"},
            {"name": "Fnatic", "league": "LEC", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1631819669150_fnc-2021-worlds.png"},
            {"name": "Team BDS", "league": "LEC", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1641292031788_Team_BDSlogo_square.png"},
            {"name": "GIANTX", "league": "LEC", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1704790322129_GX.png"},
            {"name": "Karmine Corp", "league": "LEC", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1704714951336_KC.png"},
            {"name": "Team Vitality", "league": "LEC", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1675865863968_Vitality_FullColor.png"},
            {"name": "Rogue", "league": "LEC", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1705054928404_RGE.png"},
            {"name": "Movistar KOI", "league": "LEC", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1734012609283_MKOI_FullColor_Blue.png"},
            {"name": "Team Heretics", "league": "LEC", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1672933861879_Heretics-Full-Color.png"},
            {"name": "SK Gaming", "league": "LEC", "icon": "https://am-a.akamaihd.net/image?resize=48:&f=http%3A%2F%2Fstatic.lolesports.com%2Fteams%2F1643979272144_SK_Monochrome.png"}
        ]

        for team_data in teams_data:
            team = Team.query.filter_by(name=team_data["name"]).first()
            if team:
                icon_filename = download_icon(team_data["icon"], team_data["name"])
                if icon_filename:
                    team.icon = icon_filename
                    print(f"已更新 {team.name} 的图标: {icon_filename}")
                else:
                    print(f"未能下载 {team.name} 的图标")
            else:
                print(f"未找到战队: {team_data['name']}")

        db.session.commit()
        print("所有战队图标更新完成！")

if __name__ == "__main__":
    update_team_icons() 